package com.hzcx.app.simplechat.ui.group;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.base.BaseEmptyBean;
import com.hzcx.app.simplechat.model.GroupModel;
import com.hzcx.app.simplechat.ui.friend.bean.FriendBean;
import com.hzcx.app.simplechat.ui.friend.bean.FriendCityBean;
import com.hzcx.app.simplechat.ui.group.LiveData.GroupInfoBeanLiveData;
import com.hzcx.app.simplechat.ui.group.bean.GroupInfoBean;
import com.hzcx.app.simplechat.ui.publicui.PublicRoundDialog;
import com.hzcx.app.simplechat.view.imageview.RoundImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ChatGroupMuteMemberActivity extends ChatGroupDeleteMemberActivity {

    @BindView(R.id.mute_select_all_image)
    RoundImageView muteSelectAllImage;
    @BindView(R.id.mute_select_all_text)
    TextView muteSelectAllText;

    boolean selectAll = false;

    @Override
    public int getLayout() {
        return R.layout.activity_chat_group_mute_member;
    }

    @Override
    public void initView() {
        super.initView();
        tvTitle.setText("添加禁言成员");
        ivTitleRight.setVisibility(View.GONE);
        tvTitleRight.setVisibility(View.GONE);
    }

    @OnClick({R.id.tv_title_right, R.id.tv_next, R.id.mute_select_all_image, R.id.mute_select_all_text})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.mute_select_all_text:
            case R.id.mute_select_all_image: // 全选
                selectAll = !selectAll;
                if (selectAll) {
                    muteSelectAllImage.setImageResource(R.mipmap.ic_icon_check_select);
                } else {
                    muteSelectAllImage.setImageResource(R.mipmap.ic_icon_check_normal);
                }
                if (friendData == null || friendData.size() <= 0) {
                    return;
                }
                for (int i = 0; i < friendData.size(); i++) {
                    if (friendData == null || friendData.get(i) == null || friendData.get(i).getCity() == null || friendData.get(i).getCity().size() <= 0) {
                        continue;
                    }
                    for (int j = 0; j < friendData.get(i).getCity().size(); j++) {
                        friendData.get(i).getCity().get(j).setChecked(selectAll);
                    }
                }
                setCheckedNum();
                friendAdapter.notifyDataSetChanged();
                break;

            default:
                super.viewOnClick(view);
                break;
        }
    }

    /**
     * 获取选中总人数
     */
    public int setCheckedNum() {
        int num = 0;
        for (FriendBean bean : friendData) {
            if (friendData == null || bean == null || bean.getCity() == null || bean.getCity().size() <= 0) {
                continue;
            }
            for (FriendCityBean subBean : bean.getCity()) {
                if(subBean.isChecked()) {
                    num++;
                }
            }
        }
        if (num > 0) {
            Drawable drawable = getResources().getDrawable(R.drawable.btn_main_blue_send_bg);
            tvNext.setBackground(drawable);
            tvNext.setText("完成(" + num + ")");
        } else {
            Drawable drawable = getResources().getDrawable(R.drawable.mute_select_normal_bg);
            tvNext.setBackground(drawable);
            tvNext.setText("完成");
        }
        return num;
    }

    @Override
    public void deleteMember(String ids) {
        showAddDialog(ids);
    }

    /**
     * 添加自动回复弹窗
     */
    private void showAddDialog(String ids) {
        List<String> list = new ArrayList<>();
        list.add("5分钟");
        list.add("1小时");
        list.add("1天");
        list.add("7天");
        list.add("30天");

        List<Integer> duration = new ArrayList<>();
        duration.add(5);
        duration.add(60);
        duration.add(24 * 60);
        duration.add(7 * 24 * 60);
        duration.add(30 * 7 * 24 * 60);

        PublicRoundDialog roundDialog = new PublicRoundDialog(this, "取消", list, (pos, menu) -> {
            muteGroupMembres(ids, duration.get(pos));
        });
        roundDialog.show();
    }

    public void muteGroupMembres(String ids, int duration) {
        GroupModel.muteGroupMembres(this, groupID, ids, duration, new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                GroupModel.getGroupInfo(ChatGroupMuteMemberActivity.this, groupID, new BaseObserver<GroupInfoBean>() {
                    @Override
                    protected void onSuccess(GroupInfoBean data) {
                        data.setMembersCount(data.getMembers().size());
                        data.save();
                        GroupInfoBeanLiveData.update(data);
                        hideLoading();
                        finish();
                    }
                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        hideLoading();
                        finish();
                    }
                });
            }
            @Override
            public void onFailure(Throwable e, String errorMsg) {
                hideLoading();
                showError("操作失败，请重试");
            }
        });
    }
}